<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-cpu"></i> 用户管理 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button type="success" icon="el-icon-plus" @click="Adduser">添加用户</el-button>
            </div>
            <el-table :data="tableData" border class="table" ref="multipleTable" header-cell-class-name="table-header">
                <el-table-column prop="id" label="用户ID" align="center"></el-table-column>
                <el-table-column prop="f_username" label="用户名" align="center"></el-table-column>
                <!-- <el-table-column prop="f_group_id" label="用户组"></el-table-column> -->
                <el-table-column prop="created_at" label="最近登录时间"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-setting" @click="setpassword(scope.row)">重置密码</el-button>
                        <el-button type="text" icon="el-icon-delete" @click="deluser(scope.row)">删除用户</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.page"
                    :page-size="query.pageNum"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>
        <!-- 设备关联商家 -->
        <el-dialog title="重置密码" :visible.sync="shopshow" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="新密码">
                    <el-input placeholder="请输入密码" v-model="newpassword"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="shopsaveEdit()">确 定</el-button>
            </span>
        </el-dialog>
        <!-- 新增设弹出框 -->
        <el-dialog title="添加用户" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="用户名">
                    <el-input placeholder="请输入登录用户名" v-model="username"></el-input>
                </el-form-item>
                <el-form-item label="密码">
                    <el-input placeholder="请输入密码" v-model="password"></el-input>
                </el-form-item>
                <el-form-item label="确认密码">
                    <el-input placeholder="请输入确认密码" v-model="againpassword"></el-input>
                </el-form-item>
                <!-- <el-form-item label="用户组">
                    <el-select v-model="value" placeholder="请选择用户组" @change="change">
                        <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"> </el-option>
                    </el-select>
                </el-form-item> -->
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit()">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { groupList, adminUserList, LinkShop, addAdminUser, getAllEqList, delAdminUser } from '../../../api/index';
export default {
    name: 'basetable',
    data() {
        return {
            query: {
                page: 1,
                pageNum: 15
            },
            tableData: [],
            newpassword:'',
                        multipleSelection: [],
            delList: [],
            editVisible: false,
            shopshow: false,
            pageTotal: 0,
            form: {},
            shopList: [
                {
                    label: '',
                    value: ''
                }
            ],
            options: [
                {
                    label: '',
                    value: ''
                }
            ],
            label: '',
            value: '',
            shopvalue: '',
            againpassword: '',
            username: '',
            password: '',
            groupId: '',
            shopeqid: ''
        };
    },
    created() {
        this.getData();
        //组列表
        groupList().then((res) => {
            let arr = res.data;
            this.options = arr.map((item) => {
                return {
                    label: item.f_group_name,
                    value: item.id
                };
            });
        });
    },
    methods: {
        
        //用户组选择
        change(e) {
            this.groupId = e;
            console.log(e);
        },
        // 列表展示数据
        getData() {
            adminUserList().then((res) => {
                console.log(res.data, '用户列表');
                this.tableData = res.data;
                // this.pageTotal = res.data.count || 50;
            });
        },
        // 触发搜索按钮
        handleSearch() {
            if (this.eqSn == '') {
                this.getData();
            }
            console.log(this.orderSn, '商家姓名');
            getAllEqList({
                page: 1,
                pageNum: 15,
                eqSn: this.eqSn
            }).then((res) => {
                console.log(res.data.list, '设备列表');
                this.tableData = res.data.list;
                this.pageTotal = res.data.count || 50;
            });
        },
        // 删除用户
        deluser(e) {
            console.log(e, '删除');
            delAdminUser({
                id: e.id
            }).then((res) => {
                this.$message.success(`用户删除成功`);
                this.getData();
                console.log(res, '删除');
            });
        },
        //新增用户按钮
        Adduser() {
            this.editVisible = true;
            // this.shop();
        },
        // 新增用户保存
        saveEdit() {
            if (this.username == '') {
                this.$message.warning(`请输入用户名`);
                return;
            }
            if (this.password == '') {
                this.$message.warning(`请输入密码`);
                return;
            }
            if (this.againpassword == '') {
                this.$message.warning(`请输入确认密码`);
                return;
            }
            // if (this.value == '') {
            //     this.$message.warning(`请选择用户组`);
            //     return;
            // }
            this.editVisible = false;

            addAdminUser({
                username: this.username,
                password: this.password,
                groupId: this.groupId
            }).then((res) => {
                this.$message.success(`用户创建成功`);
                console.log(res, '删除');
            });
            this.getData();
        },
        //设新密码按钮
        setpassword(e) {
            console.log(e,'设新密码按钮')
            this.newusername = e.f_username
            this.shopeqid = e.id;
            
            console.log(this.shopeqid, 'shopeqid');
            this.shopshow = true;
        },
        // 新密码保存
        shopsaveEdit() {
            if (this.newpassword == '') {
                this.$message.warning(`请输入新密码`);
                return;
            }
            this.shopshow = false;
            addAdminUser({
                username: this.newusername,
                password: this.newpassword,
                id: this.shopeqid
            }).then((res) => {
                this.$message.success(`密码修改成功`);
            });
            this.getData();
           
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'page', val);
            this.getData();
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.green {
    color: rgb(38, 141, 38);
}
.red {
    color: red;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
